<template>
  <div class="header-qvil">
    <div class="header-content">
      <div class="header-left">
        <el-button v-if="showBack" class="back-button" text @click="handleBack">
          <el-icon><arrow-left /></el-icon>
          返回
        </el-button>
      </div>
      <div class="header-center">
        <h2 class="title">{{ title }}</h2>
        <p v-if="subtitle" class="subtitle">{{ subtitle }}</p>
      </div>
      <div class="header-right">
        <template v-if="showActions">
          <el-button
            v-for="action in actions"
            :key="action.label"
            :disabled="action.disabled"
            :loading="action.loading"
            :type="action.type"
            @click="handleActionClick(action)"
          >
            <el-icon v-if="action.icon">
              <component :is="action.icon" />
            </el-icon>
            {{ action.label }}
          </el-button>
        </template>
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import { ArrowLeft } from '@element-plus/icons-vue'
import type { HeaderEvents, HeaderOptions } from './types'

const props = withDefaults(defineProps<HeaderOptions>(), {
  title: '',
  subtitle: '',
  showBack: false,
  showActions: false,
  actions: () => [],
})

const emit = defineEmits<HeaderEvents>()

// 处理返回
const handleBack = () => {
  emit('back')
}

// 处理操作点击
const handleActionClick = (action: HeaderOptions['actions'][0]) => {
  emit('action-click', action)
}
</script>

<style scoped>
.header-qvil {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
  background-color: #fff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.header-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 60px;
  padding: 0 20px;
}

.header-left {
  flex: 0 0 auto;
}

.header-center {
  flex: 1;
  text-align: center;
}

.header-right {
  flex: 0 0 auto;
  display: flex;
  gap: 12px;
}

.title {
  margin: 0;
  font-size: 18px;
  font-weight: 500;
  color: var(--el-text-color-primary);
}

.subtitle {
  margin: 4px 0 0;
  font-size: 14px;
  color: var(--el-text-color-secondary);
}

.back-button {
  display: flex;
  align-items: center;
  gap: 4px;
}
</style>
